package com.mdlib.helper.ui.fragments.sect.run;

import android.view.View;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.entity.RunStatusEntity;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/25 12:00
 * 邮箱：hqy_xz@126.com
 */

public class ApplyInstructionFragment extends BaseTitleFragment {


    @Bind(R.id.tv_apply_run_man)
    TextView mTvApplyRunMan;
    private boolean isRun = false;
    private String status = "同意并申请";

    public static ApplyInstructionFragment newInstance() {
        return new ApplyInstructionFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_apply_run_man_instruction;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);

        setTitle(R.string.apply_run_man_instruction_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });

    }

    @Override
    public void onStart() {
        super.onStart();
        getRunStatus();
    }

    private void getRunStatus() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getRunStatus()
                .compose(RxHelper.<RunStatusEntity>defaultHandleResult())
                .subscribe(new RxSubscribe<RunStatusEntity>() {
                    @Override
                    protected void onSucc(RunStatusEntity runStatusEntity) {

                        stopLoadingDialog();
                        if (EmptyUtils.isEmpty(runStatusEntity)) {
                            return;
                        }
                        int type = runStatusEntity.getStatus();
                        if (type == 1) {
                            mTvApplyRunMan.setEnabled(false);
                            status = getActivity().getResources().getString(R.string.apply_run_man_status_one);
                            mTvApplyRunMan.setText(status);
                        } else if (type == 2) {
                            mTvApplyRunMan.setEnabled(false);
                            status = getActivity().getResources().getString(R.string.apply_run_man_status_two);
                            mTvApplyRunMan.setText(status);
                        } else if (type == 3) {
                            mTvApplyRunMan.setEnabled(true);
                            status = getActivity().getResources().getString(R.string.apply_run_man_status_three);
                        } else if (type == 4) {
                            mTvApplyRunMan.setEnabled(false);
                            status = getActivity().getResources().getString(R.string.apply_run_man_status_four);
                            mTvApplyRunMan.setText(status);
                        }
                        ToastUtil.showToast(status);
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }


    @OnClick(R.id.tv_apply_run_man)
    public void onClick() {
        addFragment(ApplyRunManFragment.newInstance());
    }
}
